/*
 * @Description: 全线跟踪数据处理层
 * @Version: 1.0
 * @Date: 2024-02-29 14:05:03
 * @LastEditTime: 2024-03-20 13:42:56
 */

/**
 * 全线跟踪各机组跑空、冲套时间计算逻辑
 * @param technologicalName 
 * @param field 
 * @param enSpeed 
 * @param exSpeed 
 * @param looper 
 * @returns 
 */
export function sleeveDeclineTime(technologicalName:string,field:string,enSpeed:number,exSpeed:number,looper:number):string{
    const looperN:number = parseInt(looper.toString())
    const obj = conf[technologicalName][field]
    if(enSpeed === exSpeed) return "跑空时间 99 min"
    if(enSpeed > exSpeed) {
        let num = 0;
        const looperValue = 90 - looperN > 0 ? (90 - looperN)/100 : 0;
        const value = ((looperValue * Math.round(obj))/(enSpeed - exSpeed));
        if(Number(value) > 99) return "冲套时间 99 min"
        if( value < 1) num = 1
        return `冲套时间 ${value.toFixed(num)} min`
    } 
    if(enSpeed < exSpeed) {
        let num = 0;
        const looperValue = looperN - 5 > 0 ? (looperN - 5)/100 : 0;
        const value = ((looperValue * Math.round(obj) )/(exSpeed - enSpeed))
        if(Number(value) > 99) return "跑空时间 99 min"
        if( value < 1) num = 1
        return `跑空时间 ${value.toFixed(num)} min`
    }
    return ""
}

const conf:any = {
    CGL1870:{
        entrance:598.75,
        middle:1042.145,
        exit:565.092,
    },
    CAL1630:{
        entrance:1442.472,
        middle:1711.404,
        exit:299.784,
    },
    CAL2150:{
        entrance:1418.185,
        middle:1678.301,
        exit:314.068,
    },
    TCM2280:{
        entrance:748.62,
        middle:300,
        exit:456.8,
    }

}

export const rollingMillRealArr = ["M_505_152NI","M_505_151NI","M_505_150NI","M_505_149NI","M_505_148NI"]
export const rollingMillNextArr = ["M_406_422NI","M_406_392NI","M_406_362NI","M_406_332NI","M_406_302NI"]
const field = ["5","4","3","2","1"]
/**
 *  酸轧机组轧机张力实时数据封装
 * @param res 
 * @returns 
 */
export function rollingMillTableData(res:any):any{
    const realObj = field.reduce((acc:any,curr:any,i:any)=>{
        acc[curr] = res[rollingMillRealArr[i]] == undefined ? '0' : parseFloat(res[rollingMillRealArr[i]]).toFixed(0)
        return acc
    },{})
    realObj["chinese"] = "Current(real)"
    const nextObj = field.reduce((acc:any,curr:any,i:any)=>{
        acc[curr] = res[rollingMillNextArr[i]] == undefined ? '0' : parseFloat(res[rollingMillNextArr[i]]).toFixed(0)
        return acc
    },{})
    nextObj["chinese"] = "Next(set up)"
    const differenceObj:any = {}
    for (let index = 0; index < field.length; index++) {
        const element = field[index];
        differenceObj[element] = realObj[element] - nextObj[element];
    }
    differenceObj["chinese"] = "Difference"
    return [realObj,nextObj,differenceObj]
}

/**
 *  总张力报警跟踪判断逻辑
 * @param tensionTrackerState 
 * @param tensionData 
 * @returns 
 */
export function tensionAlarm(tensionTrackerState:Array<any>,tensionData:Array<Array<any>>){
    for (let index = 0; index < tensionTrackerState.length; index++) {
        const element = tensionTrackerState[index];
        const arr = tensionData[index];
        for (let key = 0; key < arr.length; key++) {
            const obj = arr[key];
            const value = (obj.data1 - obj.data2) / obj.data2 
            if(value > 0.2 || value <  -0.8) {
                element.state = true
                break;
            }
            element.state = false;
        }
    }
    return tensionTrackerState;
}
/**
 * 液压缸，带钢报警逻辑
 * @param deskewTrackerState 
 * @param tensionData 
 * @param setValueArr 
 * @returns 
 */
export function deskewAlarm(deskewTrackerState:Array<any>,tensionData:Array<Array<any>>,setValueArr:Array<Array<number>>){
    for (let index = 0; index < deskewTrackerState.length; index++) {
        const element = deskewTrackerState[index];
        const arr = tensionData[index];
        const setValueAr = setValueArr[index]
        for (let key = 0; key < arr.length; key++) {
            const obj = arr[key];
            if(Math.abs(obj.data1) > 10) {
                element.state = true
                break;
            }
            if(Math.abs(obj.data2) / setValueAr[key] > 0.8){
                element.state = true
                break;
            }
            element.state = false;
        }
    }
    return deskewTrackerState;
}
/**
 *  镀锌机组 液压缸，带钢报警逻辑
 * @param deskewTrackerState 
 * @param tensionData 
 * @returns 
 */
export function deskewAlarmCGL(deskewTrackerState:Array<any>,tensionData:Array<Array<any>>){
    for (let index = 0; index < deskewTrackerState.length; index++) {
        const element = deskewTrackerState[index];
        const arr = tensionData[index];
   
        for (let key = 0; key < arr.length; key++) {
            const obj = arr[key];
            if(Math.abs(obj.data1) > 10) {
                element.state = true
                break;
            }
            if(Math.abs(obj.data2) > 80){
                element.state = true
                break;
            }
            element.state = false;
        }
    }
    return deskewTrackerState;
}
/**
 *  查询coilId
 * @param zones 
 * @returns 
 */
export function SearchForCoidId(zones:any){
    const keys = Object.keys(zones);
    const Arr = []
    const color =[]
    for (let index = 0; index < keys.length; index++) {
        const element:any = keys[index];
        Arr.push(zones[element].entId )
        color.push((ColourStrategy as any)[(zones[index].ti % 10)]())
    }
    const newArr = Array.from(new Set(Arr))
    const colorArr = Array.from(new Set(color))
    const object:any ={}
    for (let index = 0; index < newArr.length; index++) {
        const element = newArr[index];
        object[element] = colorArr[index];
    }
    return {
        coilIds: Array.from(new Set(Arr)),
        colors: object
    }
}
export enum ColourType {
    ZERO = 0,
    ONE = 1,
    TWO = 2,
    THREE = 3,
    FOUR = 4,
    FIVE = 5,
    SEX = 6,
    SEVEN = 7,
    EIGHT = 8,
    NINE = 9,
    INITLINE = -1,
    INITLGRAPHICAL = -2,

}
const ColourStrategy = {
    [ColourType.ZERO]: () => ("#FF6600"),
    [ColourType.ONE]: () => ("#FF00FF"),
    [ColourType.TWO]: () => ("#CC3300"),
    [ColourType.THREE]: () => ("#0099FF"),
    [ColourType.FOUR]: () => ("#F08080"),
    [ColourType.FIVE]: () => ("#C0C0C0"),
    [ColourType.SEX]: () => ("rgba(255,128,64,255)"),
    [ColourType.SEVEN]: () => ("#C71585"),
    [ColourType.EIGHT]: () => ("rgba(147,147,0,255)"),
    [ColourType.NINE]: () => ("rgba(255,128,192,255)"),
    [ColourType.INITLINE]: () => ("#5F6368"),
    [ColourType.INITLGRAPHICAL]: () => ("#1E1E1E"),
}